//
//  AnthemViewController.m
//  GameBoard
//
//  Created by knight on 14-7-17.
//  Copyright (c) 2014年 knight. All rights reserved.
//

#import "AnthemViewController.h"

@interface AnthemViewController ()

@end

@implementation AnthemViewController

- (void) buttonPress:(id)sender
{
    [textField resignFirstResponder];
    [self loadWebPageWithString:textField.text];
}

- (NSString * ) checkUrlString:(NSString*) urlString
{
    if ([urlString hasPrefix:@"http://"]) {
        return urlString;
    }
    else{
        NSString * head = @"http://";
        urlString =[head stringByAppendingString:urlString];
        NSLog(@"检查urlString 改变了 %@",urlString);
        return urlString;
    
    }
}


- (void) loadWebPageWithString:(NSString *)urlString
{
    urlString = [self checkUrlString:urlString];
    
    NSURL * url = [NSURL URLWithString:urlString];
    
    NSLog(@"打印URL 地址：%@",urlString);
    
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    
    [webView loadRequest:request];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    webView.scalesPageToFit = YES;
    webView.delegate = self;
    activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    //加一些说明
    [activityIndicatorView setCenter:self.view.center];
    
    [activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    
    [self.view addSubview:activityIndicatorView];
    [self buttonPress:nil];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma 委托实现的方法
/**
UIWebView主要有下面几个委托方法：
1、- (void)webViewDidStartLoad:(UIWebView *)webView;开始加载的时候执行该方法。
2、- (void)webViewDidFinishLoad:(UIWebView *)webView;加载完成的时候执行该方法。
3、- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;加载出错的时候执行该方法。
**/
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [activityIndicatorView startAnimating];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{

    [activityIndicatorView stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"错误提示" message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    
    [alterview show];
}

@end
